class EventsController < ApplicationController
  before_filter :authenticate
  # GET /events
  # GET /events.xml
  def index
    
    @page = params[:page].nil? ? 1 : params[:page]
    @events = Event.all.paginate :page=>params[:page], :order=>'created_at desc', :per_page => 2
    @events = Event.all.paginate :page=>(params[:page].to_i - 1), :order=>'created_at desc', :per_page => 3 if @events.count == 0 && !params[:page].nil?

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @events }
      format.js {render 'events.js.erb'}
    end
  end

  # GET /events/1
  # GET /events/1.xml
  def show
    @event = Event.find(params[:id])
    
    @articles = @event.articles.paginate :page=>params[:page], :order=>'created_at desc', :per_page => 2
    @articles = @event.articles.paginate :page=>(params[:page].to_i - 1), :order=>'created_at desc', :per_page => 2 if @articles.count == 0 && !params[:page].nil?
    # @page = params[:page].nil? ? 1 : params[:page]
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @event }
      format.js {render 'articles.js.erb'}
    end
  end

  # GET /events/new
  # GET /events/new.xml
  def new
    @event = Event.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @event }
    end
  end

  # GET /events/1/edit
  def edit
    @event = Event.find(params[:id])
  end

  # POST /events
  # POST /events.xml
  def create
    @event = Event.new(params[:event])
    
    @event.user = current_user

    respond_to do |format|
      if @event.save
        format.html { redirect_to(@event, :notice => 'Event was successfully created.') }
        format.xml  { render :xml => @event, :status => :created, :location => @event }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /events/1
  # PUT /events/1.xml
  def update
    @event = Event.find(params[:id])

    respond_to do |format|
      if @event.update_attributes(params[:event])
        format.html { redirect_to(@event, :notice => 'Event was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def published
    @event = Event.find(params[:id])
    
    if @event.published
      @event.published = false
      @turn = "off"
      @do = "un"
      @title = "No"
    else
      @event.published = true
      @turn = "on"
      @do = ""
      @title = "Yes"
    end
    
    respond_to do |format|
      if @event.save
        format.html { redirect_to(events_url, :notice => "Article was successfully #{@do}published.") }
        format.js { render 'published.js.erb' }
      else
        format.html { redirect_to(events_url, :notice => "Article didn\'t been #{@do}published.") }
        format.js { render 'published_err.js.erb' }
      end
    end
      
  end 

  # DELETE /events/1
  # DELETE /events/1.xml
  def destroy
    @event = Event.find(params[:id])
    @event.destroy

    return nil
  
    respond_to do |format|
      format.html { redirect_to(events_url) }
      format.xml  { head :ok }
    end
  end
  
  def upload_image
    render :layout => false
  end
  
  def preview_event
    @event = Event.find(params[:eventid])
    render :layout => false
  end
end
